layui.use(['element', 'layer', 'util', 'form', 'table'], function (exports) {
    var $ = layui.jquery
        , layer = layui.layer
        , element = layui.element //Tab的切换功能，切换事件监听等，需要依赖element模块
        , util = layui.util
        , table = layui.table
        , form = layui.form;

    localStorage.removeItem("checkData");
    var wellData;
    table.render({
        elem: '#wellTable'
        , url: baseUrl + 'jcjgl/main/dzdp/getdata'
        , cellMinWidth: 100 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
       // , height: $(document).height() - $('#wellTable').offset().top - 520
        , parseData: function (res) {
            console.log(res);
            wellData = res;
            return {
                "code": 0
                , "msg": ""
                , "count": 1000
                , "data": res
            }
        }
        ,
        cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {type: 'numbers', title: '序号', fixed: 'left'}

            , {
                field: 'manholecode', width: 150,
                title: '检查井编码<div><input type="text" name="title" autocomplete="off" class="wellCode"></div>'

            }
            , {field: 'manholetype', title: '检查井类型<div><input class="wellType" type="text"/></div>'}//
            , {
                field: 'unit',
                title: '权属单位<div><input type="text" name="title" autocomplete="off" class="unitSch"></div>'
            }
            , {
                field: 'roadsectionName',
                title: '路段<div><input type="text" name="title" autocomplete="off" class="roadName"></div>'
            }
            , {
                field: 'alarmtype',
                title: '状况' +
                    '<form class="layui-form" action="">' +
                    '<div>' +
                    '<select lay-filter= "selectType" class="selectType">' +
                    '<option value="井盖状况">井盖状况</option>' +
                    '<option value="井盖打开">井盖打开</option>' +
                    '<option value="井盖恢复">井盖恢复</option>' +
                    '</select>' +
                    '</div><' +
                    '/form>',

            }
            , {
                field: 'alarmlevel',
                title: '告警级别' +
                    '<form class="layui-form" action="">' +
                    '<div>' +
                    '<select lay-filter= "selectLever" class="selectLever">' +
                    '<option value="0">一级报警</option>' +
                    '<option value="1">二级报警</option>' +
                    '<option value="2">三级报警</option>' +
                    '</select>' +
                    '</div>' +
                    '</form>',

            }
            , {
                field: 'coltime', title: '采集时间', width: 150, templet: function (d) {
                    return formateDate(d.coltime);
                }

            }

        ]]
        , done: function (res, curr, count) {
            tooBarFun();

        }
    });
    var checkData = [];
    table.on('checkbox(wellTable)', function (obj) {
        // console.log(obj.checked); //当前是否选中状态
        // console.log(obj.data); //选中行的相关数据
        // console.log(obj.type); //如果触发的是全选，则为：all，如果触发的是单选，则为：one
        // console.log(checkData);
        if (obj.type == 'all' && obj.checked == true) {
            //全选
            checkData = wellData;
            console.log(wellData);
        } else if (obj.type == 'one' && obj.checked == true) {
            //单选
            checkData.push(obj.data);
            console.log(checkData);
        } else if (obj.type == 'one' && obj.checked == false) {
            for (var i = 0; i < checkData.length; i++) {
                if (checkData[i].fid == obj.data.fid) {
                    checkData.splice(i, 1);//（下标i开始，删除1个）
                    console.log(checkData);
                }

            }

        } else if (obj.type == 'all' && obj.checked == false) {
            checkData = [];
        }

        localStorage.setItem("checkData", JSON.stringify(checkData)); //存入 参数： 1.调用的值 2.所要存入的数据

    });


    /**
     * 时间格式化
     * @param timestamp
     * @returns {string}
     */
    function formateDate(timestamp) {
        var date = new Date(timestamp);
        var y = 1900 + date.getYear();
        var m = "0" + (date.getMonth() + 1);
        var d = "0" + date.getDate();
        return y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length);
    }

    /**
     * 地图点击弹层
     */
    $('#mapWindow').click(function () {
        layer.open({
            type: 2
            , title: ['井盖分布信息', 'font-size:18px;']
            , area: ['75%', '500px']
            , offset: 'auto' //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
            , id: 'layerDemo' //防止重复弹出
            , content: 'listMap.html'
            , btnAlign: 'c' //按钮居中
            , shade: 0.8 //不显示遮罩
            , yes: function () {
                layer.closeAll();
            }
        });
    })//onclick="window.open('listMap.html')


    /**
     * 搜索事件
     * @type {string}
     */
    var codeVal = '';
    var manholetype = '';
    var unitSch = '';
    var roadName = '';
    var wellType = '';
    var wellAlarm = '';

    function tooBarFun() {
        //检查井编码搜索
        $(".wellCode").on("input", function (e) {
            //获取input输入的值
            codeVal = e.delegateTarget.value;
            manholetype = $('.wellType')[0].value;
            unitSch = $('.unitSch')[0].value;
            roadName = $('.roadName')[0].value;

            seachFun(codeVal, manholetype, unitSch, roadName);

        });
        //检查井类型搜索
        $(".wellType").on("input", function (e) {
            //获取input输入的值
            manholetype = e.delegateTarget.value;
            codeVal = $('.wellCode')[0].value;
            unitSch = $('.unitSch')[0].value;
            roadName = $('.roadName')[0].value;

            seachFun(codeVal, manholetype, unitSch, roadName);
            console.log(e.delegateTarget.value);
        });
        //权属单位搜索
        $(".unitSch").on("input", function (e) {
            //获取input输入的值
            unitSch = e.delegateTarget.value;
            codeVal = $('.wellCode')[0].value;
            manholetype = $('.wellType')[0].value;
            roadName = $('.roadName')[0].value;

            seachFun(codeVal, manholetype, unitSch, roadName);
            console.log(e.delegateTarget.value);
        });
        //路段搜索
        $(".roadName").on("input", function (e) {
            //获取input输入的值
            roadName = e.delegateTarget.value
            codeVal = $('.wellCode')[0].value;
            manholetype = $('.wellType')[0].value;
            unitSch = $('.unitSch')[0].value;

            seachFun(codeVal, manholetype, unitSch, roadName);
            console.log(e.delegateTarget.value);
        });

        //井盖状况
        form.on('select(selectType)', function (data) {
            roadName = $('.roadName')[0].value;
            codeVal = $('.wellCode')[0].value;
            manholetype = $('.wellType')[0].value;
            unitSch = $('.unitSch')[0].value;
            wellType = data.value;
            console.log(data.elem); //得到select原始DOM对象
            console.log(data.value); //得到被选中的值
            console.log(data.othis); //得到美化后的DOM对象
        });

        //告警级别
        form.on('select(selectLever)', function (data) {
            roadName = $('.roadName')[0].value;
            codeVal = $('.wellCode')[0].value;
            manholetype = $('.wellType')[0].value;
            unitSch = $('.unitSch')[0].value;
            wellType = data.value;

            wellAlarm = data.value;

            console.log(data.elem); //得到select原始DOM对象
            console.log(data.value); //得到被选中的值
            console.log(data.othis); //得到美化后的DOM对象
        });
    }

    function seachFun(obj1, obj2, obj3, obj4, obj5, obj6) {
        var arr = [];
        for (var i = 0; i < wellData.length - 3; i++) {
            //如果字符串中不包含目标字符会返回-1
            if (wellData[i].manholecode.toString().indexOf(obj1) >= 0
                && wellData[i].manholetype.indexOf(obj2) >= 0
                && wellData[i].unit.indexOf(obj3) >= 0
                && wellData[i].roadsectionName.indexOf(obj4) >= 0) {
                arr.push(wellData[i]);
            }
            //记载表格
        }

        //  return arr;
        table.render({
            elem: '#wellTable'
            //, url: 'https://entrywbzy4-vip.app.jtzny.com/jcjgl/main/dzdp/getdata'
            , cellMinWidth: 100 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , height: 512
            , parseData: function (res) {
                console.log(res);
                // wellData = res;
                return {
                    "code": 0
                    , "msg": ""
                    , "count": 1000
                    , "data": res
                }
            }
            ,
            cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {type: 'numbers', title: '序号', fixed: 'left'}

                , {
                    field: 'manholecode', width: 150,
                    title: '检查井编码<div><input type="text" name="title" autocomplete="off" class="wellCode"></div>'

                }
                , {
                    field: 'manholetype',
                    title: '检查井类型<div><input autocomplete="off"class="wellType" type="text"/></div>'
                }//
                , {
                    field: 'unit',
                    title: '权属单位<div><input type="text" name="title" autocomplete="off" class="unitSch"></div>'
                }
                , {
                    field: 'roadsectionName',
                    title: '路段<div><input type="text" name="title" autocomplete="off" class="roadName"></div>'
                }
                , {
                    field: 'alarmtype',
                    title: '状况' +
                        '<form class="layui-form" action="">' +
                        '<div>' +
                        '<select lay-filter= "selectType" class="selectType">' +
                        '<option value="井盖状况">井盖状况</option>' +
                        '<option value="井盖打开">井盖打开</option>' +
                        '<option value="井盖恢复">井盖恢复</option>' +
                        '</select>' +
                        '</div><' +
                        '/form>',

                }
                , {
                    field: 'alarmlevel',
                    title: '告警级别' +
                        '<form class="layui-form" action="">' +
                        '<div>' +
                        '<select lay-filter= "selectLever" class="selectLever">' +
                        '<option value="0">一级报警</option>' +
                        '<option value="1">二级报警</option>' +
                        '<option value="2">三级报警</option>' +
                        '</select>' +
                        '</div>' +
                        '</form>',

                }
                , {
                    field: 'coltime', title: '采集时间', width: 150, templet: function (d) {
                        return formateDate(d.coltime);
                    }
                }
            ]]
            , data: arr
            //在表格加载完毕后执行的方法
            , done: function (res, curr, count) {
                console.log('11111');
                $('.wellCode')[0].value = obj1;
                $('.wellType')[0].value = obj2;
                $('.unitSch')[0].value = obj3;
                $('.roadName')[0].value = obj4;

                //
                var $div = $("#categoryParentCode").next();
                $div.find('.layui-this').removeClass('layui-this');
                var parentCode = row.data.ParentCode;
                if(parentCode!==""){
                    $div.find('div input').val(parentCode);

                    $div.find('dl dd[lay-value="'+parentCode+'"]').addClass('layui-this');
                }else{
                    $div.find('div input').val("");
                }

                tooBarFun();
            }
        });

    }
})